import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Fyysika_uus extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	Kysimused kysimuste_database;
	Kyssad_vazzad_paneel kysvas;
	Kysimus selectedQuestion = new Kysimus();
	JLabel kysimus, kysvastlabel, protsent, tyhi, tyhi1;
	JPanel valiteema_ja_editor;
	JButton  valiteema, toimeta, tee_uus_fail, laeblackboardist;
	Editor editor;
	JLabel[] leibelid;
	ArrayList<JLabel> pildiNupp;
	JFileChooser fc;
	boolean menyyNupudTehtud;
	int count, lambikas, kysimustearv, protsentarv, kyspikkus;
	double oiged;

	public Fyysika_uus() {
		protsentarv = 0;
		oiged = 0;
		kysimustearv = 0;
		menyyNupudTehtud = false;
		setLayout(new BorderLayout());
		setBackground(new Color(191, 239, 245));
		valiteema_ja_editor = new JPanel();
		add(valiteema_ja_editor, BorderLayout.NORTH);
		valiteema = new JButton("Soorita teste");
		valiteema.addActionListener(this);
		valiteema_ja_editor.add(valiteema);
		toimeta = new JButton("Toimeta faili");
		toimeta.addActionListener(this);
		valiteema_ja_editor.add(toimeta);
		tee_uus_fail = new JButton("Tee uus fail");
		tee_uus_fail.addActionListener(this);
		valiteema_ja_editor.add(tee_uus_fail);
		laeblackboardist = new JButton("Lae Blackboardist kopeeritud fail");
		laeblackboardist.addActionListener(this);
		valiteema_ja_editor.add(laeblackboardist);
		fc = new JFileChooser();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "Soorita teste") {
			fc.resetChoosableFileFilters();
			fc.setFileFilter(new FileNameExtensionFilter("xml failid", "xml"));
			int returnVal = fc.showOpenDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) 
				try {
					kysimuste_database = new Kysimused(false);
					String fail = fc.getSelectedFile().getAbsolutePath();
					kysimuste_database = kysimuste_database.loeXML_data(fc.getSelectedFile().getCanonicalPath(), kysimuste_database);
					if (kysvas!=null) this.remove(kysvas);
					if (editor!=null) this.remove(editor);
					fail = fail.substring(fail.lastIndexOf("\\") + 1, fail.length());
					kysvas = new Kyssad_vazzad_paneel(kysimuste_database, false);
					kysvas.fortsykkel();
					this.add(kysvas);
					this.repaint();
				} catch (IOException e1) {
					e1.printStackTrace();
       			}
			/* ArrayList<File> failid = new ArrayList<File>();
			File actual = new File(".");
			for (File f : actual.listFiles()) if (f.isFile()) if (f.toString().contains(".xml")) failid.add(f);
			Object[] selectionValues = new Object[failid.size()];
			for (int i = 0; i < failid.size(); i++) selectionValues[i] = failid.get(i).getName();
			Object selection = JOptionPane.showInputDialog(null,"Vali küsimuste-vastuste fail!", "Teemavalik",JOptionPane.QUESTION_MESSAGE, null, selectionValues, 3);
			if(selection == null) return; 
			kysimuste_database = new Kysimused(false);
			kysimuste_database = kysimuste_database.loeXML_data(selection.toString(), kysimuste_database); 
			*/
		} // end if(e.getActionCommand() == "Soorita teste")

		if (e.getActionCommand() == "Lae Blackboardist kopeeritud fail") { 
			fc.resetChoosableFileFilters();
			fc.setFileFilter(new FileNameExtensionFilter("txt failid", "txt"));
			int returnVal = fc.showOpenDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
				kysimuste_database = new Kysimused(false);
				String fail = fc.getSelectedFile().getAbsolutePath();
				kysimuste_database = kysimuste_database.laeKyssadBlackboardist(fail, kysimuste_database);
				if (kysvas!=null) this.remove(kysvas);
				if (editor!=null) this.remove(editor);
				fail = fail.substring(fail.lastIndexOf("\\") + 1, fail.length());
				this.add(editor = new Editor(kysimuste_database, fail));
			}

			/* ArrayList<File> failid = new ArrayList<File>();
			File actual = new File(".");
			for (File f : actual.listFiles()) if (f.isFile()) if (f.toString().contains(".txt")) failid.add(f);
			Object[] selectionValues = new Object[failid.size()];
			for (int i = 0; i < failid.size(); i++) selectionValues[i] = failid.get(i).getName();
			Object selection = JOptionPane.showInputDialog(null,"Vali BlackBoardi kopipasta fail!", "Teemavalik",JOptionPane.QUESTION_MESSAGE, null, selectionValues, 3);
			if(selection == null) return; 
			kysimuste_database = new Kysimused(false);
			kysimuste_database = kysimuste_database.laeKyssadBlackboardist(selection.toString(), kysimuste_database);
			this.add(editor = new Editor(kysimuste_database, selection.toString().substring(0, selection.toString().length()-3)+"xml"));
			*/
		} // end if(e.getActionCommand() == "Lae Blackboardist"

		if (e.getActionCommand() == "Toimeta faili") {
			fc.resetChoosableFileFilters();
			fc.setFileFilter(new FileNameExtensionFilter("XML failid", "xml"));
			int returnVal = fc.showOpenDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) 
				try {
					kysimuste_database = new Kysimused(false);
					String fail = fc.getSelectedFile().getCanonicalPath();
					kysimuste_database = kysimuste_database.loeXML_data(fail, kysimuste_database);
					if (kysvas!=null) this.remove(kysvas);
					if (editor!=null) this.remove(editor);
					fail = fail.substring(fail.lastIndexOf("\\") + 1, fail.length());
           			this.add(editor = new Editor(kysimuste_database, fail));
				} catch (IOException e1) {
					e1.printStackTrace();
				}
		} // end if (e.getActionCommand() == "Toimeta faili")

		if (e.getActionCommand() == "Tee uus fail") {
			if (kysvas!=null) {
				this.remove(kysvas);
			}
			if (editor!=null) {
				this.remove(editor);
			}
			kysimuste_database = new Kysimused(false);
			kysimuste_database.add_kysimus(new Kysimus());
			this.add(editor = new Editor(kysimuste_database, "uus_fail.xml"));
		}
		this.revalidate();
	}
}
